// pages/merchant/goods/add/add.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsInfo:{},
    categoryList:[],
    index:null,
    spuImage:"https://thirdwx.qlogo.cn/mmopen/vi_32/ibtGdb0eibSzGA94VbQHrraDia5QWDibH8OONXuT1DeL0wZ3GZx9CiavZU33gxyib7tqY54UegXLhzAtAibAicvRMMbcYw/132"
  },
  formSubmit: function(e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    app.wxRequest('POST', '/spu/update', e.detail.value, (res) => { 
      if(200==res.code){ 
      wx.navigateBack({current: e.detail.value.goodsCategoryId})
      }else{
        app.showMsg(res.msg)
      }
    }, (err) => { 
      app.showMsg("接口异常")
    }) 
  },
  updateGoodsInfo:function(spuId){
    app.wxRequest('GET', '/spu/getSpuInfoBySpuId/'+spuId, null, (res) => { 
      if(200==res.code){ 
        this.setData({ 
          goodsInfo: res.data
        }) 
      }else{
        app.showMsg(res.msg)
      }
    }, (err) => { 
      app.showMsg("接口异常")
    }) 
  },
  getCategoryList:function(goodsCategoryId){
    app.wxRequest('GET', '/goodsCategory/getList', null, (res) => { 
      if(200==res.code){ 
        // for (let i = 0; i <this.data.categoryList.length; i++) {
        //   console.log(i);
        // }
        // console.log("fff44"+this.data.categoryList)
        const arr = res.data;
        let categoryIndex=null;
        arr.find(function (item, index, arr) {
          console.log("0000="+item.goodsCategoryId);
          console.log("0000999="+index);
            if(goodsCategoryId == item.goodsCategoryId){
              categoryIndex=index; 
              return item;
            }
        })
        console.log("index"+JSON.stringify(categoryIndex))
        this.setData({ 
          categoryList: arr,
          index:categoryIndex
        });
        
      }else{
        app.showMsg(res.msg)
      }
    }, (err) => { 
      app.showMsg("接口异常")
    }) 
  },
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("spuId:"+options.spuId)
    console.log("goodsCategoryId:"+options.goodsCategoryId)
    this.updateGoodsInfo(options.spuId)
    this.getCategoryList(options.goodsCategoryId)

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow(options) {
      this.setData({
        index:this.data.index
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})